home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / OLDCRTL.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  5KB  |  173 lines

  1. /*       SCCS Id: @(#)oldcrtl.c   3.0      90/05/24
  2. /*        Pat Rankin  May'90                                       */
  3. /* VMS NetHack support, not needed for vms 4.6,4.7,or 5.x.        */
  4.  
  5. #ifdef VERYOLD_VMS
  6. /*
  7.  * The following routines are used by NetHack but were not available
  8.  * from the C Run-Time Library (VAXCRTL) prior to VMS V4.6.
  9.  *
  10.  *      atexit, memcmp, memcpy, qsort, rename, vprintf, vsprintf
  11.  *
  12.  * Most of them are implemented here, but others will have to be worked
  13.  * around in another fashion [such as '#define USE_OLDARGS' (even though
  14.  * <varargs.h> is available) to avoid the need for vprintf & vsprintf].
  15.  *
  16.  */
  17. #define REG register
  18.  
  19. #ifndef SUPPRESS_MEM_FUNCS
  20. /* note: hand optimized for VAX (hardware pre-decrement & post-increment) */
  21.  
  22. /* void *memset(void *, int, size_t) -- fill chunk of memory.
  23. */
  24. char *memset( dst, fil, cnt )
  25. REG char *dst;
  26. REG char  fil;
  27. REG int   cnt;
  28. {
  29.     char *dst_p = dst;
  30.     while ( --cnt >= 0 )
  31.     *dst++ = fil;
  32.     return dst_p;
  33. }
  34.  
  35. /* void *memcpy(void *, const void *, size_t) -- copy chunk of memory.
  36. */
  37. char *memcpy( dst, src, cnt )       /*[functionally equivalent to memmove()]*/
  38. REG char *dst;
  39. REG char *src;
  40. REG int   cnt;
  41. {
  42.     char *dst_p = dst;
  43.     if ( src == dst || cnt <= 0 ) {
  44.     ;       /* do nothing */
  45.     } else if ( dst < src || dst >= src + cnt ) {
  46.     while ( --cnt >= 0 )
  47.         *dst++ = *src++;
  48.     } else {    /* work backwards */
  49.     dst += cnt,  src += cnt;
  50.     while ( --cnt >= 0 )
  51.         *--dst = *--src;
  52.     }
  53.     return dst_p;
  54. }
  55.  
  56. /* void *memchr(const void *, int, size_t) -- search for a byte.
  57. */
  58. char *memchr( buf, byt, len )
  59. REG char *buf;
  60. REG char  byt;
  61. REG int   len;
  62. {
  63.     while ( --len >= 0 )
  64.     if ( *buf++ == byt )    /* found */
  65.         return --buf;
  66.     return (char *)0;       /* not found */
  67. }
  68.  
  69. /* int memcmp(const void *, const void *, size_t) -- compare two chunks.
  70. */
  71. int memcmp( buf1, buf2, len )
  72. REG char *buf1;
  73. REG char *buf2;
  74. REG int   len;
  75. {
  76.     while ( --len >= 0 )
  77.     if ( *buf1++ != *buf2++ )
  78.         return (*--buf1 - *--buf2);
  79.     return 0;   /* buffers matched */
  80. }
  81. #endif /*!SUPPRESS_MEM_FUNCS*/
  82.  
  83.  
  84. #ifndef SUPPRESS_ATEXIT
  85. /* int atexit(void (*)(void)) -- register an exit handler.
  86. */
  87. #define MAX_EXIT_FUNCS 32       /* arbitrary (32 matches VAX C v3.x docs) */
  88. struct _ex_hndlr { long reserved, (*routine)(), arg_count, *arg1_addr; };
  89. static int ex_cnt = 0;          /* number of handlers registered so far */
  90. static struct { long dummy_arg; struct _ex_hndlr handler;   /*(black box)*/
  91.        } ex_data[MAX_EXIT_FUNCS];       /* static handler data */
  92.  
  93. int atexit( function )
  94.     int (*function)();          /* note: actually gets called with 1 arg */
  95. {
  96.     if ( ex_cnt < MAX_EXIT_FUNCS ) {
  97.     ex_data[ex_cnt].dummy_arg = 0;  /* ultimately receives exit reason */
  98.     ex_data[ex_cnt].handler.reserved  = 0;
  99.     ex_data[ex_cnt].handler.routine   = function;
  100.     ex_data[ex_cnt].handler.arg_count = 1;          /*(required)*/
  101.     ex_data[ex_cnt].handler.arg1_addr = &ex_data[ex_cnt].dummy_arg;
  102.     SYS$DCLEXH( &ex_data[ex_cnt].handler);  /* declare exit handler */
  103.     return ++ex_cnt;        /*(non-zero)*/
  104.     } else
  105.     return 0;
  106. }
  107. #endif /*!SUPPRESS_ATEXIT*/
  108.  
  109.  
  110. #ifndef SUPPRESS_RENAME
  111. /* int rename(const char *, const char *) -- rename a file (on same device).
  112. */
  113. #ifndef EVMSERR
  114. #include <errno.h>
  115. #define C$$TRANSLATE(status)    (errno = EVMSERR,  vaxc$errno = (status))
  116. #endif
  117.  
  118. int rename( old_name, new_name )
  119.     char *old_name;
  120.     char *new_name;
  121. {
  122.     struct _dsc { int len; char *adr; } old_dsc, new_dsc;
  123.     unsigned long status, LIB$RENAME_FILE();
  124.  
  125.     /* put strings into descriptors and call run-time library routine */
  126.     old_dsc.len = strlen( old_dsc.adr = old_name );
  127.     new_dsc.len = strlen( new_dsc.adr = new_name );
  128.     status = LIB$RENAME_FILE( &old_dsc, &new_dsc);  /* omit optional args */
  129.     if ( !(status & 1) ) {      /* even => failure */
  130.     C$$TRANSLATE(status);
  131.     return -1;
  132.     } else                      /*  odd => success */
  133.     return 0;
  134. }
  135. #endif /*!SUPPRESS_RENAME*/
  136.  
  137.  
  138. #ifndef SUPPRESS_QSORT
  139. /* void qsort(void *, size_t, size_t, int (*)()) -- sort arbitrary collection.
  140. */
  141. void qsort( base, count, size, compare )
  142.     char *base;
  143.     int   count;
  144. REG int   size;
  145.     int (*compare)();
  146. {
  147. REG int   i, cmp;
  148. REG char *next, *prev, *tmp = 0;
  149.     char  wrk_buf[512], *malloc();      /* assume no alloca() available */
  150.  
  151.     /* just use a shuffle sort (tradeoff between efficiency & simplicity) */
  152.     /*  [Optimal if already sorted; worst case when initially reversed.]  */
  153.     for ( next = base, i = 1;  i < count;  i++ ) {
  154.     prev = next,  next += size;             /* increment front pointer */
  155.     if ( (cmp = (*compare)( next, prev)) < 0 ) {
  156.         /* found element out of order; move other(s) up then re-insert it */
  157.         if ( !tmp )  tmp = (size > sizeof wrk_buf ? malloc(size) : wrk_buf);
  158.         memcpy( tmp, next, size);           /* save smaller element */
  159.         while ( cmp < 0 ) {
  160.         memcpy( prev + size, prev, size);   /* move larger elem. up */
  161.         prev -= size;                   /* decrement back pointer */
  162.         cmp = (prev >= base ? (*compare)( tmp, prev) : 0);
  163.         }
  164.         memcpy( prev + size, tmp, size);    /* restore small element */
  165.     }
  166.     }
  167.     if ( tmp != 0 && tmp != wrk_buf )  free(tmp);
  168.     return;
  169. }
  170. #endif /*!SUPPRESS_QSORT*/
  171.  
  172. #endif /*VERYOLD_VMS*/
  173.